package com.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Created by
 *  predicate接口默认放过and可以将两个条件同时成立则为true   or两个条件只要一个为true则为true  negate方法取反的意思
 * @author touni
 * @date 2019/4/29
 * @time 11:10
 */
public class DemoPredicate {

    public static Boolean chkStr(String str, Predicate<String> pre){
        return pre.test(str);
    }
    public static Boolean testAndMethod(String str,Predicate<String> pre1,Predicate<String> pre2){
        return pre1.and(pre2).test(str);
    }

    /**
     * 复杂形的多条件的判断
     * @param strArr
     * @param pre1
     * @param pre2
     * @return
     */
    public static List<String> test(String[] strArr, Predicate<String> pre1, Predicate<String> pre2){
        List<String> list = new ArrayList<>();
        for (String str:strArr) {
            if (pre1.and(pre2).test(str)) {
                list.add(str);
            }
        }
        return list;
    }








    public static void main(String[] args) {
        Boolean boo = chkStr("abc", (str) -> {
            return  "abc".equals(str);
        });
        System.out.println(boo);

        Boolean testAndMethod = testAndMethod("lalalala", (str) -> {
            return str.length() == 1;
        }, (str) -> {
            return str.contains("a");
        });
        System.out.println(testAndMethod);

        String[] strArr = {"王五为，男","是是是，女","张三，男","事实上，男"};
        List<String> list = test(strArr, (str) -> {
            return str.split("，")[0].length() == 2;
        }, (str) -> {
            return str.split("，")[1].equals("男");
        });
        System.out.println(list.toString());
    }










}
